var express = require('express');
var router = express.Router();

const MyQuery = require('../utils/db')

//引入配置文件
const {API_URL} = require('../utils/config')

/* GET home page. */
router.get('/demo1', function(req, res, next) {
    res.send('首页模块')
});

//首页轮播图
router.get('/banners',async (req,res,next)=>{
    
    let data = {}//最终要返回的数据格式  msg,status,result
    //1. 写sql
    let sql = ` SELECT *,CONCAT('${API_URL}/uploads/banners/',image_src) AS image_src FROM e_banners WHERE isshow=1  `
    //2. 执行sql
    let {result} = await MyQuery(sql) //{ err,result }

    //3. 要处理成规范、格式化的json数据
    data = {
        msg:'OK',
        status:200,
        result
    }
    res.send( data )
})

//首页课程列表
//SELECT id,cid,title,price,CONCAT('http://localhost:3000/uploads/courses/',image_src) AS image_src,type,type_name,area_name,subject_name FROM e_course WHERE type= 1 LIMIT 8
router.get('/courseinfo',async (req,res,next)=>{
    
    let {type = 1} = req.query
    let size = type == 1? 8:6 //如果type为1就是同步，同步是8条。  type为2就是精品，精品是8条。

    let data = {}//最终要返回的数据格式  msg,status,result
    //1. 写sql
    let sql = ` SELECT id,cid,title,price,CONCAT('${API_URL}/uploads/courses/',image_src) AS image_src,type,type_name,area_name,subject_name FROM e_course WHERE type= ${type} LIMIT ${size} `
    //2. 执行sql
    let {result} = await MyQuery(sql) //{ err,result }

    //3. 要处理成规范、格式化的json数据
    data = {
        msg:'OK',
        status:200,
        result
    }
    res.send( data )
})


module.exports = router;
